home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python152_Src.lha / Python152_Source / Amiga / _close.c < prev    next >
C/C++ Source or Header  |  1998-12-25  |  2KB  |  93 lines

  1. RCS_ID_C="$Id: _close.c,v 4.1 1994/09/29 23:09:02 jraja Exp $";
  2. /*
  3.  *      _close.c - close a file (SAS/C)
  4.  *
  5.  *      Copyright © 1994 AmiTCP/IP Group, 
  6.  *                       Network Solutions Development Inc.
  7.  *                       All rights reserved.
  8.  */
  9.  
  10. #include <ios1.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <dos.h>
  14. #include <proto/dos.h>
  15. #include <errno.h>
  16. #include <bsdsocket.h>
  17. #include "libcheck.h"
  18.  
  19. int 
  20. __close(int fd)
  21. {
  22.   struct UFB *ufb;
  23.  
  24.   /*
  25.    * Check for the break signals
  26.    */
  27.   __chkabort();
  28.  
  29.   /*
  30.    * Find the ufb *
  31.    */
  32.   if ((ufb = __chkufb(fd)) == NULL) {
  33.     /* __chkufb sets the errno to EBADF */
  34.     return -1;
  35.   }
  36.  
  37.   /*
  38.    * Check if close is not needed
  39.    */
  40.   if ((ufb->ufbflg & (UFB_NC | UFB_CLO)) != UFB_NC) {
  41.  
  42.     /*
  43.      * Empty flags mean empty ufb
  44.      */
  45.     if (ufb->ufbflg == 0) {
  46.       errno = EBADF;
  47.       return -1;
  48.     }
  49.  
  50.     /*
  51.      * Close the file
  52.      */
  53.     if (!(ufb->ufbflg & UFB_SOCK) && ufb->ufbfh != NULL) {
  54.       Close(ufb->ufbfh);
  55.       
  56.       /*
  57.        * Remove the file if it was temporary
  58.        */
  59.       if (ufb->ufbflg & UFB_TEMP && ufb->ufbfn != NULL) 
  60.     remove(ufb->ufbfn);
  61.     }
  62.  
  63.   }
  64.  
  65.   /*
  66.    * Free the file name
  67.    */
  68.   if (ufb->ufbfn != NULL) {
  69.     free(ufb->ufbfn);
  70.     ufb->ufbfn = NULL;
  71.   }
  72.  
  73.   /*
  74.    * Clear the flags to free this ufb
  75.    */
  76.   ufb->ufbflg = 0;
  77.   ufb->ufbfh = NULL; /* just in case */
  78.  
  79.   /* 
  80.    * closes the socket OR the file mark
  81.    */
  82.  
  83.   /* needs bsdsocket.library for CloseSocket call -- I.J. */
  84.   /* Can safely assume it is opened at the time we need to do a */
  85.   /* CloseSocket() call (how could you create a socket otherwise?), */
  86.   /* so a call to checksocketlib() is not needed. */
  87.   /* THIS IS VERY IMPORTANT OTHERWISE IMPORTANT ERROR DATA IS DISCARDED. */
  88.   if(SocketBase) CloseSocket(fd);
  89.   
  90.   return 0;
  91. }
  92.  
  93.